/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.mime.ParsedAddress;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import java.util.ListIterator;
/*     */ 
/*     */ 
/*     */ public class SenderList
/*     */ {
/*     */   private static final int MAX_PARTICIPANT_COUNT = 8;
/*     */   private List<ParsedAddress> mParticipants;
/*     */   private boolean mIsElided;
/*     */   private long mLastDate;
/*     */   private int mSize;
/*     */   private static final String FN_EMAIL = "a";
/*     */   private static final String FN_PERSONAL = "p";
/*     */   private static final String FN_DISPLAY = "d";
/*     */   public SenderList() {}
/*     */   
/*     */   public static class RefreshException
/*     */     extends Exception
/*     */   {
/*     */     private static final long serialVersionUID = 7137768259442997280L;
/*     */     
/*     */     public RefreshException(String message)
/*     */     {
/*  31 */       super();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public SenderList(Message msg)
/*     */   {
/*  44 */     String sender = msg.getSender();
/*  45 */     if ((sender != null) && (!sender.trim().equals(""))) {
/*  46 */       this.mParticipants = new ArrayList(8);
/*  47 */       this.mParticipants.add(new ParsedAddress(sender).parse());
/*  48 */       this.mLastDate = msg.getDate();
/*     */     }
/*  50 */     this.mSize = 1;
/*     */   }
/*     */   
/*     */   public SenderList(Message[] msgs) {
/*  54 */     this(msgs == null ? null : Arrays.asList(msgs));
/*     */   }
/*     */   
/*     */   public SenderList(List<Message> msgs) {
/*  58 */     if ((msgs == null) || (msgs.isEmpty())) {
/*  59 */       return;
/*     */     }
/*  61 */     Collections.sort(msgs, new MailItem.SortDateAscending());
/*  62 */     this.mLastDate = ((Message)msgs.get(msgs.size() - 1)).getDate();
/*  63 */     this.mSize = msgs.size();
/*     */     
/*  65 */     for (ListIterator<Message> lit = msgs.listIterator(this.mSize); (lit.hasPrevious()) && (!this.mIsElided);) {
/*  66 */       String sender = ((Message)lit.previous()).getSender();
/*  67 */       if ((sender != null) && (!sender.trim().equals("")))
/*     */       {
/*  69 */         ParsedAddress pa = new ParsedAddress(sender).parse();
/*  70 */         if ((this.mParticipants == null) || (!this.mParticipants.contains(pa)))
/*     */         {
/*  72 */           if (this.mParticipants == null) {
/*  73 */             (this.mParticipants = new ArrayList(8)).add(pa);
/*  74 */           } else if (this.mParticipants.size() >= 8) {
/*  75 */             this.mIsElided = true;
/*     */           } else
/*  77 */             this.mParticipants.add(pa); }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public SenderList add(Message msg) throws SenderList.RefreshException {
/*  83 */     String sender = msg.getSender();
/*  84 */     if ((sender == null) || (sender.trim().equals(""))) {
/*  85 */       this.mSize += 1;
/*  86 */       return this;
/*     */     }
/*     */     
/*  89 */     long date = msg.getDate();
/*  90 */     if (date < this.mLastDate) {
/*  91 */       throw new RefreshException("appended message predates existing last message");
/*     */     }
/*  93 */     this.mLastDate = date;
/*  94 */     this.mSize += 1;
/*     */     
/*  96 */     ParsedAddress pa = new ParsedAddress(sender).parse();
/*  97 */     if (this.mParticipants == null) {
/*  98 */       (this.mParticipants = new ArrayList(8)).add(pa);
/*     */     } else {
/* 100 */       this.mParticipants.remove(pa);
/* 101 */       this.mParticipants.add(0, pa);
/* 102 */       while (this.mParticipants.size() > 8) {
/* 103 */         this.mParticipants.remove(8);
/* 104 */         this.mIsElided = true;
/*     */       }
/*     */     }
/*     */     
/* 108 */     return this;
/*     */   }
/*     */   
/*     */ 
/* 112 */   public int size() { return this.mSize; }
/* 113 */   public boolean isElided() { return this.mIsElided; }
/*     */   
/* 115 */   public List<ParsedAddress> getLastAddresses() { if ((this.mParticipants == null) || (this.mParticipants.isEmpty()))
/* 116 */       return Collections.emptyList();
/* 117 */     List<ParsedAddress> addrs = new ArrayList(this.mParticipants);
/* 118 */     if (addrs.size() > 1)
/* 119 */       Collections.reverse(addrs);
/* 120 */     return addrs;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static ParsedAddress importAddress(Metadata meta)
/*     */   {
/* 129 */     if (meta == null)
/* 130 */       return null;
/* 131 */     ParsedAddress pa = new ParsedAddress(meta.get("a", null), meta.get("p", null));
/* 132 */     pa.firstName = meta.get("d", null);
/* 133 */     return pa;
/*     */   }
/*     */   
/*     */   public static SenderList parse(String encoded) throws ServiceException, SenderList.RefreshException {
/* 137 */     return parse(new Metadata(encoded));
/*     */   }
/*     */   
/*     */   public static SenderList parse(Metadata meta) throws ServiceException, SenderList.RefreshException {
/* 141 */     if (meta.getMap("f", true) != null) {
/* 142 */       return parseDeprecated(meta);
/*     */     }
/* 144 */     SenderList sl = new SenderList();
/* 145 */     sl.mSize = ((int)meta.getLong("no"));
/* 146 */     sl.mLastDate = meta.getLong("ld", 0L);
/* 147 */     sl.mIsElided = meta.getBool("X");
/* 148 */     MetadataList entries = meta.getList("en", true);
/* 149 */     if ((entries != null) && (!entries.isEmpty())) {
/* 150 */       sl.mParticipants = new ArrayList(entries.size());
/* 151 */       for (int i = 0; i < entries.size(); i++)
/* 152 */         sl.mParticipants.add(importAddress(entries.getMap(i)));
/*     */     }
/* 154 */     return sl;
/*     */   }
/*     */   
/*     */ 
/*     */   private static SenderList parseDeprecated(Metadata meta)
/*     */     throws ServiceException, SenderList.RefreshException
/*     */   {
/* 161 */     int size = (int)meta.getLong("no");
/* 162 */     MetadataList entries = meta.getList("en", true);
/* 163 */     if ((entries != null) && (entries.size() != size - 1)) {
/* 164 */       throw new RefreshException("switching from old SenderList to new SenderList requires refresh");
/*     */     }
/* 166 */     SenderList sl = new SenderList();
/* 167 */     sl.mSize = size;
/* 168 */     sl.mLastDate = meta.getLong("ld", 0L);
/* 169 */     sl.mIsElided = false;
/* 170 */     sl.mParticipants = new ArrayList(entries == null ? 1 : size);
/* 171 */     if ((entries != null) && (!entries.isEmpty())) {
/* 172 */       for (int i = 0; i < entries.size(); i++)
/* 173 */         sl.mParticipants.add(importAddress(entries.getMap(i)));
/*     */     }
/* 175 */     sl.mParticipants.add(importAddress(meta.getMap("f")));
/* 176 */     return sl;
/*     */   }
/*     */   
/*     */   private static Metadata exportAddress(ParsedAddress pa) {
/* 180 */     if (pa == null)
/* 181 */       return null;
/* 182 */     Metadata meta = new Metadata();
/* 183 */     meta.put("a", pa.emailPart);
/* 184 */     meta.put("p", pa.personalPart);
/* 185 */     meta.put("d", pa.firstName);
/* 186 */     return meta;
/*     */   }
/*     */   
/*     */   public String toString() {
/* 190 */     Metadata meta = new Metadata();
/* 191 */     meta.put("no", this.mSize);
/* 192 */     meta.put("ld", this.mLastDate);
/* 193 */     meta.put("X", this.mIsElided);
/* 194 */     if ((this.mParticipants != null) && (!this.mParticipants.isEmpty())) {
/* 195 */       MetadataList entries = new MetadataList();
/* 196 */       for (ParsedAddress pa : this.mParticipants)
/* 197 */         entries.add(exportAddress(pa));
/* 198 */       meta.put("en", entries);
/*     */     }
/* 200 */     return meta.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/SenderList.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */